package com.changsi12.web;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;

import com.changsi12.dao.GradeDao;
import com.changsi12.model.Grade;
import com.changsi12.util.DbUtil;
import com.changsi12.util.ExcelUtil;
import com.changsi12.util.ResponseUtil;

public class GradeExportServlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	DbUtil dbUtil=new DbUtil();
	GradeDao gradeDao=new GradeDao();
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String gradeName=request.getParameter("gradeName");
		if(gradeName==null){
			gradeName="";
		}
		Grade grade = new Grade();
		grade.setGradeName(gradeName);
		Connection con = null;
		try {
			// 创建新的Excel工作簿
			Workbook workbook=new HSSFWorkbook();
			String headers[]={"编号","班级名称","班级描述"};
			
			con = dbUtil.getCon();
			ResultSet rs=gradeDao.gradeList(con, null, grade);
			ExcelUtil.fillExcelData(rs, workbook, headers);
			ResponseUtil.export(response, workbook, "导出excel");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
